--[[
    Author          ZhengFeng
    Time            2020.12.05
    Description     宠物蛋开出宠物界面
]]--
require "Common/define"
require "Logic/PetManager"

local Page_PetMainAddPets = UIBase.New(PanelResNames.Page_PetMainAddPets)
local this = Page_PetMainAddPets
local partListIndex = 0
local petSelectedIndex = 1
local selectedPetInfo = nil

function this:Init()
    UIBase.Init(this)
end

function this:OnCreate()
    self.Btn_BGClose = self:GetChild("Btn_BGClose")
    self:RegisterOnClick(self.Btn_BGClose,self.onClickClose)
    self.Txt_PetName = self:GetChild("Txt_PetName")
    self.Btn_TurnLeft = self:GetChild("Btn_TurnLeft")
    self:RegisterOnClick(self.Btn_TurnLeft,self.onClickTurnLeft)
    self.Btn_TurnRight = self:GetChild("Btn_TurnRight")
    self:RegisterOnClick(self.Btn_TurnRight,self.onClickTurnRight)
    self.List_Part = self:GetChild("List_Part")
    self.List_Part.itemRenderer = this.renderPartListItem
    self.Model_Pet = self:GetChild("Model_Pet")
    -- self.EffectWrapTarget = self:GetChild("SpecialEffect").wrapTarget
    
end

function this:OnEnable()
    ListenEvent(Config.EventType.RefreshPetList,this.onRefreshPetList)
    petSelectedIndex = 1
    --this:showSpecialEffect()
    this:showPet()
end

function this:OnDisable()
    CloseEvent(Config.EventType.RefreshPetList,this.onRefreshPetList)
    self.List_Part.numItems = 0
    --this:closeTimer()
    this:removePetModel()
end
function this:OnDestroy()
    PetManager.NewAddPetIds = {}
    this:removePetModel()
end

function this:onClickClose()
    -- if self.EffectWrapTarget.activeSelf then
    --     this:showPet()
    -- else

    if petSelectedIndex < #PetManager.NewAddPetIds then
        petSelectedIndex = petSelectedIndex + 1
        this:showPet()
    else
        BrokerManager.isCanClick = true
        petSelectedIndex = 1
        selectedPetInfo = nil
        PetManager.NewAddPetIds = {}
        this:removePetModel()
        closeUI(PanelResNames.Page_PetMainAddPets)
        if PetManager.isFirstGetPetGuide then
            PetManager.isFirstGetPetGuide = nil
            logicMgr.GuideMgr.AddTrigger(logicMgr.GuideMgr.TriggerType.petFirstGet,nil,nil,nil,true)
        end
    end
end

function this:onClickTurnLeft()
    if petSelectedIndex > 1 then
        petSelectedIndex = petSelectedIndex -1
        this:showPet()
    end
end
function this:onClickTurnRight()
    if petSelectedIndex < #PetManager.NewAddPetIds then
        petSelectedIndex = petSelectedIndex + 1
        this:showPet()
    end
end
function this.onRefreshPetList()
    --this:showSpecialEffect()
    this:showPet()
    --获得新飞飞音效
    AudioManager.Play(2019)
end
-- function this:showSpecialEffect()
--     if #PetManager.NewAddPetIds > 0 then
--         if this.petTimer == nil then
--             self.Txt_PetName.text = ""
--             self.EffectWrapTarget:SetActive(true)
--             Util.PlayAction(self.EffectWrapTarget, "idle", false)
--             local _duration = PetManager.NewPetSpecialEffectDuration
--             this.petTimer = utimer.timeCall(function ()
--                 this:showPet()
--             end,_duration,1)
--         else
--             this:showPet()
--         end
--     end
-- end
-- function this:closeTimer()
--     if this.petTimer then
--         utimer.remove(this.petTimer)
--     end
--     this.petTimer = nil
-- end

function this:showPet()
    --获得新飞飞音效
    AudioManager.Play(2019)
    -- self.EffectWrapTarget:SetActive(false)
    if #PetManager.NewAddPetIds > 0 then
        selectedPetInfo = PetManager.GetPetInfoByPetId(PetManager.NewAddPetIds[petSelectedIndex])
        self.Txt_PetName.text = selectedPetInfo.petName

        partListIndex = 0
        self.List_Part.numItems = #selectedPetInfo.genes
        this:showPetModel()

        local _leftTouchable = true
        local _rightTouchable = true
        if petSelectedIndex == 1 then
            _leftTouchable = false
        end
        if petSelectedIndex == #PetManager.NewAddPetIds then
            _rightTouchable = false
        end
        this.Btn_TurnLeft.touchable = _leftTouchable
        this.Btn_TurnLeft.enabled = _leftTouchable
        this.Btn_TurnRight.touchable = _rightTouchable
        this.Btn_TurnRight.enabled = _rightTouchable
    end
end
function this:renderPartListItem(obj)
    partListIndex = partListIndex + 1
    local _geneId = selectedPetInfo.genes[partListIndex]
    local _geneConfig = G.dataTable["t_elvesgene"]
    local ctrlItem = CtrlManager.GetCtrl(PanelNames.PetPartItem);
    ctrlItem.initData(obj, _geneId);
    --local _geneInfo = _geneConfig[tostring(_geneId)]
    --local _partName = getLanguage(_geneInfo.f_GeneName)
    --
    --local Txt_title = obj:GetChild("title")
    --Txt_title.text = _partName
    --
    --local _petMutationLevel = _geneInfo.f_MutationLevel
    --obj:GetController("level"):SetSelectedIndex(_petMutationLevel-1)
end

--显示宠物模型
function this:removePetModel()
    if this.PetModelGO then
        destroy(this.PetModelGO)
    end
    this.PetModelGO = nil
end
function this:showPetModel()
    local _petInfo = selectedPetInfo
    if _petInfo and #_petInfo.genes == PetManager.PetPart.Ornament then
        local _petModelInfo = {}
        _petModelInfo.parentGO = this.Model_Pet
        _petModelInfo.petId = _petInfo.petId
        if this.PetModelGO and this.goWrapper then
            _petModelInfo.modelGO = this.PetModelGO
            _petModelInfo.modelWrapper = this.goWrapper
            PetManager.PetModelRefresh(_petModelInfo,_petInfo.genes,true)
        else
            this:removePetModel()
            resMgr:LoadModelPrefab(PetManager.PetModelPrefab,function(go)
                this.PetModelGO = newObject(go)

                local com = this.PetModelGO:GetComponent("SkeletonGraphic")

             if this.PetModelGO and com then
                 com.material = resMgr.Pet_Copy_Mat
                 com.material:SetFloat("_StencilComp",8)
                 com.material:SetFloat("_Stencil",1);
             end
             
                _petModelInfo.modelGO = this.PetModelGO
                local _modelWrapper = this.goWrapper
                this.goWrapper = GoWrapper.New()
                _petModelInfo.modelWrapper = this.goWrapper
                PetManager.PetModelFirstSet(_petModelInfo,_petInfo.genes,true,Vector3.New(0,0,0),Vector3.New(3,3,1))
                if _modelWrapper then
                    _modelWrapper:Dispose()
                end
            end)
        end
    end
end

return this